package controller

import (
	"mini-app/config/mysql"
	"mini-app/internal/app/web/service"
	"mini-app/internal/dao"

	"192.168.1.75/go-pkg/response"
	"github.com/gin-gonic/gin"
)

type AdCoupon struct{}

// Create 创建广告优惠券
func (AdCoupon) Create(c *gin.Context) {
	var params struct {
		Name          string  `json:"name"`
		SalesOff      float32 `json:"sales_off"`
		StartAt       int64   `json:"start_at"`
		EndAt         int64   `json:"end_at"`
		DeviceGroupID string  `json:"device_group_id"`
	}
	c.ShouldBindJSON(&params)

	err := service.AdCoupon{}.Create(c.Request.Context(), params.Name, params.SalesOff, params.StartAt, params.EndAt, params.DeviceGroupID)
	response.JSON(c, gin.H{}, err)
}

// GetByTimeRange 获取时间范围内的优惠券
func (AdCoupon) GetCoupons(c *gin.Context) {
	var params struct {
		StartTime       int64  `json:"start_time"`
		EndTime         int64  `json:"end_time"`
		DeviceGroupID   string `json:"device_group_id"`
		DeviceGroupName string `json:"device_group_name"`
		LocationIDs     string `json:"location_ids"`
	}
	c.ShouldBindJSON(&params)

	coupons, err := service.AdCoupon{}.GetCouponsByContition(c.Request.Context(), params.StartTime, params.EndTime, params.DeviceGroupID, params.DeviceGroupName, params.LocationIDs)

	type CouponResponse struct {
		ID              string  `json:"id"`
		Name            string  `json:"name"`
		SalesOff        float32 `json:"sales_off"`
		StartAt         int64   `json:"start_at"`
		EndAt           int64   `json:"end_at"`
		DeviceGroupID   string  `json:"device_group_id"`
		DeviceGroupName string  `json:"device_group_name"`
		LocationIDs     string  `json:"location_ids"`
		Status          int     `json:"status"`
	}
	couponResponses := make([]CouponResponse, len(coupons))

	db := mysql.NewDB()
	// 获取所有优惠券关联的设备组名称
	for i, coupon := range coupons {
		if coupon.DeviceGroupID != "" {
			groupName, _ := dao.PublishDeviceGroup{}.GetGroupNameByID(c.Request.Context(), db, coupon.DeviceGroupID)
			couponResponses[i] = CouponResponse{
				ID:              coupon.ID,
				Name:            coupon.Name,
				SalesOff:        coupon.SalesOff,
				StartAt:         coupon.StartAt,
				EndAt:           coupon.EndAt,
				DeviceGroupID:   coupon.DeviceGroupID,
				DeviceGroupName: groupName,
				LocationIDs:     coupon.LocationIDs,
				Status:          coupon.Status,
			}
		}

	}

	response.JSON(c, gin.H{
		"coupons": couponResponses,
		"total":   len(coupons),
	}, err)
}

func (AdCoupon) ModifyByID(c *gin.Context) {
	var params struct {
		ID            string  `json:"id"`
		Name          string  `json:"name"`
		SalesOff      float32 `json:"sales_off"`
		StartAt       int64   `json:"start_at"`
		EndAt         int64   `json:"end_at"`
		DeviceGroupID string  `json:"device_group_id"`
	}
	c.ShouldBindJSON(&params)

	err := service.AdCoupon{}.ModifyByID(c.Request.Context(), params.ID, params.Name, params.SalesOff, params.StartAt, params.EndAt, params.DeviceGroupID)
	response.JSON(c, gin.H{}, err)

}

func (AdCoupon) DeleteByID(c *gin.Context) {
	var params struct {
		ID string `json:"id"`
	}
	c.ShouldBindJSON(&params)

	err := service.AdCoupon{}.DeleteByID(c.Request.Context(), params.ID)
	response.JSON(c, gin.H{}, err)

}
